<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="型号" prop="object">
        <el-input
          v-model="queryParams.object"
          placeholder="请输入型号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功率" prop="power">
        <el-input
          v-model="queryParams.power"
          placeholder="请输入功率"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="光束角" prop="beamAngle">
        <el-input
          v-model="queryParams.beamAngle"
          placeholder="请输入光束角"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="色温" prop="cloler">
        <el-input
          v-model="queryParams.cloler"
          placeholder="请输入色温"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显色指数" prop="colorRendering">
        <el-input
          v-model="queryParams.colorRendering"
          placeholder="请输入显色指数"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="防护等级" prop="levelProtection">
        <el-input
          v-model="queryParams.levelProtection"
          placeholder="请输入防护等级"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="电压" prop="voltage">
        <el-input
          v-model="queryParams.voltage"
          placeholder="请输入电压"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="调光方式" prop="dimmingMode">
        <el-input
          v-model="queryParams.dimmingMode"
          placeholder="请输入调光方式"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <!--    
  
        <el-form-item label="创建时间" style="width: 308px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          v-hasPermi="['system:prodect:add']"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:prodect:update']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:prodect:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Upload"
          @click="handleImport"
          v-hasPermi="['system:prodect:import']"
          >導入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:prodect:export']"
          >導出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Close" @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="specificationList"
      :border="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="型号"
        prop="object"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="功率"
        prop="power"
        v-if="specificationList.some((item) => item.power)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="色温"
        prop="cloler"
        v-if="specificationList.some((item) => item.cloler)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="尺寸"
        prop="size"
        v-if="specificationList.some((item) => item.size)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="光束角"
        :show-overflow-tooltip="true"
        v-if="specificationList.some((item) => item.beamAngle)"
        prop="beamAngle"
      />
      <el-table-column
        label="开槽尺寸"
        :show-overflow-tooltip="true"
        v-if="specificationList.some((item) => item.openSlotSize)"
        prop="openSlotSize"
      />
      <el-table-column
        label="显色指数"
        prop="colorRendering"
        v-if="specificationList.some((item) => item.colorRendering)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="防护等级"
        prop="levelProtection"
        v-if="specificationList.some((item) => item.levelProtection)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="输入电压"
        prop="voltage"
        v-if="specificationList.some((item) => item.voltage)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="调光方式"
        prop="dimmingMode"
        v-if="specificationList.some((item) => item.dimmingMode)"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="Landed"
        v-hasPermi="['system:prodect:bkhCost']"
        prop="bkhCost"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="成本"
        v-hasPermi="['system:prodect:bahengCost']"
        prop="bahengCost"
        :show-overflow-tooltip="true"
      >
        <template #header>
          <el-tooltip content="汇率：7" placement="top">
            <span>成本</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column
        label="设计师(BKH直营)"
        v-hasPermi="['system:prodect:stylistCost']"
        prop="stylistCost"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="零售牌價(NTD)"
        v-hasPermi="['system:prodect:terminalCost']"
        prop="terminalCost"
        :show-overflow-tooltip="true"
      />

       <el-table-column
        label="經銷商價(NTD)"
        v-hasPermi="['system:prodect:dealer']"
        prop="dealer"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="建商價(自採)(NTD)"
        v-hasPermi="['system:prodect:army']"
        prop="army"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="建商價(經銷商轉介)(NTD)"
        v-hasPermi="['system:prodect:dealerArmy']"
        prop="dealerArmy"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="设计报价(经销商开发一)(NTD)"
        v-hasPermi="['system:prodect:stylistCostOne']"
        prop="stylistCostOne"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="设计报价(经销商开发二)(NTD)"
        v-hasPermi="['system:prodect:stylistCostTwo']"
        prop="stylistCostTwo"
        :show-overflow-tooltip="true"
      /> -->


      <el-table-column
        label="修改报价时间"
        :show-overflow-tooltip="true"
        prop="updatebkhcostTime"
      >
      </el-table-column>
      <!-- <el-table-column label="图片" align="center" prop="image" >
        <template #default="scope">
          <image-preview :src="scope.row.url" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="创建时间" :show-overflow-tooltip="true"  prop="createTime" >
        </el-table-column> -->
      <el-table-column label="创建人" prop="nickname"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:prodect:update']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:prodect:delete']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form
        ref="addclientele"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="类型选择:" prop="object">
          <el-select
            style="width: 97%"
            clearable
            v-model="form.specificationType"
            placeholder="选择类型"
          >
            <el-option
              v-for="item in specification_type"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="型号:" prop="object">
          <el-input v-model="form.object" placeholder="请输入型号" />
        </el-form-item>

        <el-form-item
          label="尺寸:"
          prop="size"
          v-if="
            form.specificationType == '燈帶及硬燈條' ||
            form.specificationType == '電源驅動' ||
            form.specificationType == '電源驅動' ||
            form.specificationType == '配件及型材'
          "
        >
          <el-input v-model="form.size" placeholder="请输入尺寸" />
        </el-form-item>

        <el-form-item
          label="功率:"
          prop="power"
          v-if="form.specificationType !== '配件及型材'"
        >
          <el-input v-model="form.power" placeholder="请输入功率" />
        </el-form-item>

        <el-form-item
          label="色温:"
          prop="cloler"
          v-if="
            form.specificationType == '燈具' ||
            form.specificationType == '燈帶及硬燈條'
          "
        >
          <el-input v-model="form.cloler" placeholder="请输入色温" />
        </el-form-item>

        <!-- <el-form-item label="光通量:" prop="luminous">
            <el-input v-model="form.luminous" placeholder="请输入光通量" />
            </el-form-item> -->

        <el-form-item
          label="光束角:"
          prop="beamAngle"
          v-if="
            form.specificationType == '燈具' ||
            form.specificationType == '燈帶及硬燈條'
          "
        >
          <el-input v-model="form.beamAngle" placeholder="请输入光束角" />
        </el-form-item>

        <el-form-item
          label="显色指数:"
          prop="colorRendering"
          v-if="
            form.specificationType == '燈具' ||
            form.specificationType == '燈帶及硬燈條'
          "
        >
          <el-input
            v-model="form.colorRendering"
            placeholder="请输入显色指数"
          />
        </el-form-item>

        <el-form-item
          label="防护等级:"
          prop="levelProtection"
          v-if="form.specificationType !== '配件及型材'"
        >
          <el-input
            v-model="form.levelProtection"
            placeholder="请输入防护等级"
          />
        </el-form-item>

        <el-form-item
          label="输入电压:"
          prop="voltage"
          v-if="form.specificationType !== '配件及型材'"
        >
          <el-input v-model="form.voltage" placeholder="请输入电压" />
        </el-form-item>

        <el-form-item
          label="开槽尺寸:"
          prop="openSlotSize"
          v-if="form.specificationType == '配件及型材'"
        >
          <el-input v-model="form.openSlotSize" placeholder="请输入开槽尺寸" />
        </el-form-item>

        <el-form-item
          label="调光方式:"
          prop="dimmingMode"
          v-if="form.specificationType !== '配件及型材'"
        >
          <el-input v-model="form.dimmingMode" placeholder="请输入调光方式" />
        </el-form-item>

        <el-form-item label="Landed:" prop="bkhCost">
          <el-input v-model="form.bkhCost" placeholder="请输入Landed" />
        </el-form-item>

        <el-form-item label="成本:" prop="bahengCost">
          <el-input v-model="form.bahengCost" placeholder="请输入成本" />
        </el-form-item>

        <el-form-item label="设计师:" prop="stylistCost">
          <el-input v-model="form.stylistCost" placeholder="请输入设计师(BKH直营)" />
        </el-form-item>

         <el-form-item label="零售牌價:" prop="terminalCost">
          <el-input v-model="form.terminalCost" placeholder="请输入零售牌價" />
        </el-form-item>

        <el-form-item label="經銷商價(NTD):" prop="dealer">
          <el-input v-model="form.dealer" placeholder="请输入經銷商價" />

        </el-form-item>

        <el-form-item label="建商價(自採):" prop="army">
          <el-input v-model="form.army" placeholder="请输入建商價(自採)" />
        </el-form-item>

        <el-form-item label="建商價(經銷商轉介):" prop="dealerArmy">
          <el-input v-model="form.dealerArmy" placeholder="请输入建商價(經銷商轉介)" />
        </el-form-item>

        <el-form-item label="设计报价(经销商开发一):" prop="stylistCostOne">
          <el-input v-model="form.stylistCostOne" placeholder="请输入设计报价(经销商开发一)" />
        </el-form-item>

        <el-form-item label="设计报价(经销商开发二):" prop="stylistCostTwo">
          <el-input v-model="form.stylistCostTwo" placeholder="请输入设计报价(经销商开发二)" />
        </el-form-item> 
 


    
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      :title="templateTitle"
      v-model="templateDialogVisible"
      width="400px"
    >
      <el-select
        v-model="selectedTemplate"
        placeholder="选择模板"
        style="width: 100%"
      >
        <el-option
          v-for="item in specification_type"
          :key="item.value"
          :label="item.label"
          :value="item.label"
        />
      </el-select>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="downloadSelectedTemplate">下载</el-button>
          <el-button @click="templateDialogVisible = false">取消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 用户導入对话框 -->
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        :data="uploadParams"
        drag
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <div class="el-upload__tip">
              <el-checkbox
                v-model="upload.updateSupport"
              />是否更新已经存在的用户数据
            </div>
            <span>仅允许導入xls、xlsx格式文件。</span>
            <el-link
              type="primary"
              :underline="false"
              style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate"
              >下载模板</el-link
            >
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
   
   <script setup name="case">
import {
  listspecification,
  addspecification,
  getspecification,
  updatespecification,
  delspecification,
} from "@/api/business/specification.js";

import { ref } from "vue";
import { getToken } from "@/utils/auth";

const router = useRouter();
const { proxy } = getCurrentInstance();
const selectedTemplate = ref(null);

const { crm_colour, specification_type } = proxy.useDict(
  "crm_colour",
  "specification_type"
);

//这个是導入时候搜索框的参数
const header = ref([{}]);
//照片的url和色温
const contacts = ref([
  {
    url: "",
  },
]);
// 初始化格式化的选项数据
const formattedOptions = ref([]);
const specificationList = ref([]);
const open = ref(false);
const opens = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const specificationIds = ref([]);
const crmpictureIds = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const loadAll = ref([]);
const route = useRoute();
// 上传参数
const uploadParams = reactive({
  prodectId: route.params.Id,
});

/*** 用户導入参数 */
const upload = reactive({
  // 是否显示弹出层（用户導入）
  open: false,
  // 弹出层标题（用户導入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/specification/importData",
});

// 返回按钮
function handleClose() {
  const obj = { path: "/specification/prodect/add" };
  proxy.$tab.closeOpenPage(obj);
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    prodectId: route.params.Id,
  },
  rules: {
    object: [{ required: true, message: "型号不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const templateDialogVisible = ref(false);

/** 導入按钮操作 */
function handleImport() {
  upload.title = "用户導入";
  upload.open = true;
}

/** 下载模板操作 */
function importTemplate() {
  templateDialogVisible.value = true;
  // proxy.download("specification/importTemplate", {
  // }, `Series_template_${new Date().getTime()}.xlsx`);
}

// 下载选定的模板
function downloadSelectedTemplate() {
  if (selectedTemplate.value == "燈具") {
    // 这里添加下载逻辑，例如调用 API 下载选定的模板

    proxy.download(
      "specification/importTemplate",
      {},
      `燈具_${new Date().getTime()}.xlsx`
    );
    templateDialogVisible.value = false; // 关闭对话框
  } else if (selectedTemplate.value == "燈帶及硬燈條") {
    proxy.download(
      "specification/importTemplates",
      {},
      `燈帶及硬燈條_${new Date().getTime()}.xlsx`
    );
    templateDialogVisible.value = false; // 关闭对话框
  } else if (selectedTemplate.value == "電源驅動") {
    proxy.download(
      "specification/importTemplatess",
      {},
      `電源驅動_${new Date().getTime()}.xlsx`
    );
    templateDialogVisible.value = false; // 关闭对话框
  } else if (selectedTemplate.value == "配件及型材") {
    proxy.download(
      "specification/importTemplatesss",
      {},
      `配件及型材_${new Date().getTime()}.xlsx`
    );
    templateDialogVisible.value = false; // 关闭对话框
  }
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert(
    "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
      response.msg +
      "</div>",
    "導入结果",
    { dangerouslyUseHTMLString: true }
  );
  getList();
};
/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
}

/** 查询列表 */
function getList() {
  loading.value = true;
  listspecification(queryParams.value, dateRange.value)
    .then((response) => {
      specificationList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .catch((error) => {
      loading.value = false;
    });
}

// 删除图片和颜色
function removeContact(index) {
  contacts.value.splice(index, 1);
}
// 添加图片和颜色
function addContact() {
  contacts.value.push({
    url: "",
    colour: "",
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  let specificationId = row.specificationId || specificationIds.value;
  if (row.specificationId == null || specificationIds.value == null) {
    specificationId = 0;
  }
  proxy.$modal
    .confirm('是否确认删除系列id为"' + id + '"的数据项?')
    .then(function () {
      return delspecification(id, specificationId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 導出按钮操作 */
function handleExport() {
  proxy.download(
    "specification/export",
    {
      ...queryParams.value,
    },
    `specification_${new Date().getTime()}.xlsx`
  );
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  specificationIds.value = selection.map((item) => item.specificationId);
  crmpictureIds.value = selection.map((item) => item.crmpictureId);

  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function reset() {
  form.value = {};
  contacts.value = [{}];
  proxy.resetForm("addclientele");
}
/** 添加角色 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加单身";
}
/** 修改角色 */
function handleUpdate(row) {
  reset();

  const id = row.id || ids.value;
  getspecification(id).then((response) => {
    form.value = response.data;
    debugger;
    if (
      response.data.crmspecificationpicture &&
      response.data.crmspecificationpicture.length > 0
    ) {
      contacts.value = response.data.crmspecificationpicture;
    } else {
      contacts.value = [{ id: 0 }];
    }
    open.value = true;
    // 将 row 中的 countiesCities 通过 '-' 分割并复制到 countiesCities 数组中
    title.value = "修改参数";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["addclientele"].validate((valid) => {
    if (valid) {
      // 合并数据
      const mergedData = {
        ...form.value,
        prodectId: route.params.Id,
        crmspecificationpicture: contacts.value,
      };
      if (form.value.id != undefined) {
        updatespecification(mergedData).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addspecification(mergedData).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          uploadParams.seriesId = response.data;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
//取消按钮
function openscancel() {
  opens.value = false;
}

getList();
</script>
  
  <style scoped>
.linkman-input-group {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.linkman-input-group .el-input {
  flex: 1;
  margin-right: 10px;
}
</style>